<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_common.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_warehouse.php');


if ($_REQUEST['act'] == 'delete_warehouse')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}

	$warehouse_id=intval($_REQUEST['warehouse_id']);

 	//判断仓库是否存在，是否已被删除
	$warehouse_info=is_warehouse_exists($warehouse_id);
	if(!$warehouse_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_warehouse_not_exist'];
		die($json->encode($result));
	}
	else{
		
		//检查是否有出入库单关联到这个仓库，如果有，那么不能删除仓库
		$sql="select count(*) as num from ".$ecs->table('erp_warehousing')." where warehouse_id='".$warehouse_id."'";
		$num=$db->getOne($sql);
		
		if($num>0)
		{
			$result['error']=1;
			$result['message']=$_LANG['erp_warehouse_has_warehousing'];
			die($json->encode($result));			
		}
		
		$sql="select count(*) as num from ".$ecs->table('erp_delivery')." where warehouse_id='".$warehouse_id."'";
		$num=$db->getOne($sql);		
		
		if($num>0)
		{
			$result['error']=2;
			$result['message']=$_LANG['erp_warehouse_has_delivery'];
			die($json->encode($result));			
		}		
		
		$sql="delete from ".$ecs->table('erp_warehouse')." where warehouse_id='".$warehouse_id."'";
		$db->query($sql);
		
		$result['error']=0;
		die($json->encode($result));
	}
}

elseif ($_REQUEST['act'] == 'change_is_valid')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	if(empty($_REQUEST['warehouse_id']))
	{
		$result['error']=2;
		$result['message']=$_LANG['erp_wrong_parameter'];
		die($json->encode($result));
	}
	
	$warehouse_id=intval($_REQUEST['warehouse_id']);

 	//判断仓库是否存在，是否已被删除
	$warehouse_info=is_warehouse_exists($warehouse_id);
	if(!$warehouse_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_warehouse_not_exist'];
		die($json->encode($result));
	}
	else{
		
		$is_valid=$warehouse_info['is_valid'];
		if($is_valid==1)
		{
			$sql="update ".$ecs->table('erp_warehouse')." set is_valid='0' where warehouse_id='".$warehouse_id."'";
			$db->query($sql);
			
			$result['error']=0;
			$result['is_valid']=0;
			die($json->encode($result));
		}
		elseif($is_valid==0)
		{
			$sql="update ".$ecs->table('erp_warehouse')." set is_valid='1' where warehouse_id='".$warehouse_id."'";
			$db->query($sql);
			
			$result['error']=0;
			$result['is_valid']=1;
			die($json->encode($result));			
		}
	}
}
elseif ($_REQUEST['act'] == 'change_description')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	if(empty($_REQUEST['warehouse_id']))
	{
		$result['error']=2;
		$result['message']=$_LANG['erp_wrong_parameter'];
		die($json->encode($result));
	}
	
	$warehouse_id=intval($_REQUEST['warehouse_id']);
	$new_description=$_REQUEST['new_description'];

 	//判断仓库是否存在，是否已被删除
	$warehouse_info=is_warehouse_exists($warehouse_id);
	if(!$warehouse_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_warehouse_not_exist'];
		die($json->encode($result));
	}
	else{
		$sql="update ".$ecs->table('erp_warehouse')." set description='".$new_description."' where warehouse_id='".$warehouse_id."'";
		$db->query($sql);
		
		$result['error']=0;
		die($json->encode($result));
	}
}
elseif ($_REQUEST['act'] == 'change_admin')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$warehouse_id=intval($_REQUEST['warehouse_id']);
	$admin_id=intval($_REQUEST['admin_id']);

 	//判断仓库是否存在，是否已被删除
	$warehouse_info=is_warehouse_exists($warehouse_id);
	if(!$warehouse_info)
	{
		$result['error']=3;
		$result['message']=$_LANG['erp_warehouse_not_exist'];
		die($json->encode($result));
	}
	else{
		$sql="update ".$ecs->table('erp_warehouse')." set admin_id ='".$admin_id."' where warehouse_id='".$warehouse_id."'";
		$db->query($sql);
		
		$result['error']=0;
		die($json->encode($result));
	}
}


elseif($_REQUEST['act'] == 'add_warehouse')
{
  include('../includes/cls_json.php');
	$json  = new JSON;
	
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}	
	
	
  $_POST['new_warehouse'] = json_str_iconv($_POST['new_warehouse']);
  
 	//检查是否有编辑仓库的权限
	if(!admin_priv('erp_sys_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	} 
    
  $warehouse_info=$json->decode($_POST['new_warehouse']);

  $sql="insert into ".$ecs->table('erp_warehouse')." set name='".$warehouse_info->name."'";
  $sql.=", admin_id='".$warehouse_info->admin_id."'";
	$sql.=", description='".$warehouse_info->description."',is_valid='1'";
	 
  $db->query($sql);
  $result['error']=0;
  die($json->encode($result));
}


?>